package com.ruoyi.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author yangyi
 */
@Component
@Slf4j
public class FieldFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "createId", Long.class, SecurityUtils.getUserId());
        this.strictInsertFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateBy",null);
        metaObject.setValue("updateId",null);
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
        this.strictUpdateFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());
    }

}
